package cz.muni.fi.pb138.videoweb2013.backend;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

/**
 *  This class is providing transform method for execute XSLT transformation
 * 
 * @author Martina
 */
public class XSLT {
    
    /**
     * Transform XML in string into html string through xslt
     * 
     * @return result   String with a transformed xml from databaze
     */
    public String transform() throws TransformerConfigurationException, TransformerException    {
        MediumManager mm = new MediumManagerImpl();
        String result = null;
        
        try {
        StringReader reader;
        reader = new StringReader(mm.getXML());
        StringWriter writer = new StringWriter();
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(
            new javax.xml.transform.stream.StreamSource(XSLT.class.getClassLoader().getResource("").getPath()+"../../"+"transforming-into-tables.xsl"));

        transformer.transform(
            new javax.xml.transform.stream.StreamSource(reader), 
            new javax.xml.transform.stream.StreamResult(writer));

        result = writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return result;
        }
    }

        
}